/*
** Copyright (c) 2010 by Lorimark Solutions, LLC.
** All Rights Reserved.
** Send inquiries to: info@lorimarksolutions.com
**
*/

#include <eviesystems.h>
#include <font3x5.h>
#include <mega48serial.h>

#define LED (1<<PD5)
#define DB1 (1<<PD4)
#define DB2 (1<<PD3)

#define FLPDB1 PORTD ^= DB1
#define TOGDB1 FLPDB1; /* _delay_us(1); */ FLPDB1

#define FLPDB2 PORTD ^= DB2
#define TOGDB2 FLPDB2; /* _delay_us(1); */ FLPDB2

#define LED_FLASH_LONG  250
#define LED_FLASH_SHORT 15

uint16 ledFlash;
uint8  ledState;

ISR(TIMER1_OVF_vect)
{
  if( !ledFlash-- )
  {
//    PORTD ^= LED;

    if( ledState )
    {
      PORTD     = PIND | LED;
      ledFlash  = LED_FLASH_LONG;
      ledState  = 0;
    }
    else
    {
      PORTD     = PIND & ~LED;
      ledFlash  = LED_FLASH_SHORT;
      ledState  = 1;
    }
  }
}

void init_timer(void)
{

  sei();

  TCCR1A = 
    (0b00   << COM1A0) | // 6.Compare Output Mode for A
    (0b00   << COM1B0) | // 4.Compare Output Mode for B 
    (0b00   << WGM10)  ; // 0.Waveform Generation Mode

  TCCR1B = 
    (0b0    << ICNC1)  | // 7.Input Capture Noise Canceler
    (0b0    << ICES1)  | // 6.Input Capture Edge Select
    (0b00   << WGM12)  | // 3.Waveform Generation Mode
    (0b001  << CS10)   ; // 0.Clock Select (1=no prescaling)

  TCCR1C =
    (0b0    << FOC1A)  | // 7.Force Output Compare for A
    (0b0    << FOC1B)  ; // 6.Force Output Compare for B

  TIMSK1 = 
    (0b0    << ICIE1)  | // 5.Input Capture Interrupt Enable
    (0b0    << OCIE1B) | // 2.Output Compare B Match Interrupt Enable
    (0b0    << OCIE1A) | // 1.Output Compare A Match Interrupt Enable
    (0b1    << TOIE1)  ; // 0.Overflow Interrupt Enable

//  OCR1AH = (20000>>8);
//  OCR1AL = (20000&255);

}



/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

/*

    00000000000000001111111111111111222222222222222233333333333333334444444444444444555555555555555566666666666666667777777777777777
    0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
   +--------------------------------------------------------------------------------------------------------------------------------+
000|                                                                                                                                |
 01|                                                                                                                                |
 02|                                                                                                                                |
 03|                                                                                                                                |
 04|                                                                                                                                |
 05|                                                                                                                                |
 06|                                                                                                                                |
 07|                                                                                                                                |
108|                                                                                                                                |
 09|                                                                                                                                |
 0A|                                                                                                                                |
 0B|                                                                                                                                |
 0C|                                                                                                                                |
 0D|                                                                                                                                |
 0E|                                                                                                                                |
 0F|                                                                                                                                |
210|
 11|
 12| *  *** *** *   *** *** *** *** ***
 13|**    *   * * * *   *     * * * * *
 14| *  ***  ** *** *** ***   *  *  ***
 15| *  *     *   *   * * *   * * *   *
 16|*** *** ***   * *** ***   * ***   *
 17|******************************************************************************************************************************* |
318|                                                                                                                                |
 19|***                                                             *** ***                                     *** *** *** *** *** |
 1A|*** ***                                 ***             *** *** *** *** ***                 *** *** *** *** *** *** *** *** *** |
 1B|*** *** *** *** ***         *** *** *** *** ***         *** *** *** *** *** ***         *** *** *** *** *** *** *** *** *** *** |
 1C|*** *** *** *** ***         *** *** *** *** ***     *** *** *** *** *** *** ***         *** *** *** *** *** *** *** *** *** *** |
 1D|*** *** *** *** *** ***     *** *** *** *** ***     *** *** *** *** *** *** ***     *** *** *** *** *** *** *** *** *** *** *** |
 1E|*** *** *** *** *** ***     *** *** *** *** ***     *** *** *** *** *** *** ***     *** *** *** *** *** *** *** *** *** *** *** |
 1F|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***     *** *** *** *** *** *** *** *** *** *** *** |
420|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 21|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 22|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 23|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 24|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 25|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 26|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 27|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
528|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 29|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 2A|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 2B|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 2C|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 2D|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 2E|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 2F|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
630|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 31|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 32|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 33|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 34|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 35|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 36|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 37|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
738|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 39|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 3A|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 3B|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 3C|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 3D|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 3E|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
 3F|*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
   +--------------------------------------------------------------------------------------------------------------------------------+
    00000000000000001111111111111111222222222222222233333333333333334444444444444444555555555555555566666666666666667777777777777777
    0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF

    00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
     0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F

*/

#define SET(p,b)  p  |=  (b)
#define CLR(p,b)  p  &= ~(b)
#define TST(p,b)  (p &   (b))

#define LCD_DELAY_500  asm volatile ("nop"); \
  asm volatile ("nop"); \
  asm volatile ("nop"); \
  asm volatile ("nop"); \
  asm volatile ("nop"); \
  asm volatile ("nop"); \
  asm volatile ("nop"); \
  asm volatile ("nop");

#define LCD_DELAY       1
#define LCD_DATA_DDR    DDRC
#define LCD_DATA        PORTC
#define LCD_CONTROL_DDR DDRB
#define LCD_CONTROL     PORTB
#define LCD_RST         (1<<PB0)
#define LCD_CS2         (1<<PB1)
#define LCD_CS1         (1<<PB2)
#define LCD_E           (1<<PB3)
#define LCD_RW          (1<<PB4)
#define LCD_DI          (1<<PB5)

uint8 chip_lcd;
uint8 segment_lcd;

void delay_lcd(void)
{
  _delay_us(3);
}

/*!
** \brief Select a Chip
**
** Since most everything is zero-based in C and in nearly
**  all other systems, I have written this chip selector
**  to be zero-based also.  Sending a chip==0 selects the
**  first chip, sending a chip==1 selects the second chip.
**  Sending anything else selects no chip.
**
**
*/
void chipSelect_lcd( uint8 chip )
{
  /*
  ** Bring the enable pin low
  **
  */
  PORTB &= ~LCD_E;

  /*
  ** Wait a bit before doing anything.
  **
  */
  delay_lcd();

  /*
  ** If the chip has not changed, then we 
  **  can quit early.
  **
  */
  if( chip_lcd == chip )
  {
    return;
  }

  /*
  ** Remember the chip for next time.
  **
  */
  chip_lcd = chip;

  /*
  ** Set the chip selects.  Note that the chip selects
  **  will not change if the chip itself has not changed.
  **
  */
  switch( chip_lcd )
  {
    case 0:
    {
      PORTB &= ~LCD_CS2;
      PORTB |=  LCD_CS1;
      break;
    }

    case 1:
    {
      PORTB &= ~LCD_CS1;
      PORTB |=  LCD_CS2;
      break;
    }
  }

  delay_lcd();
}

// Purpose:       Reads a byte of data from the specified chip
// Ouputs:        A byte of data read from the chip
uint8 readByte_lcd(void)
{
  /*
  ** temp holders for the data from the lcd
  **
  */
  uint8 data1;
  uint8 data2;

  /*
  ** Make sure the read/write is READ.
  **
  */
  PORTB = PINB | LCD_RW;

  /*
  ** Make the data port an input
  **
  */
  DDRC &= 0b11000000;
  DDRD &= 0b11110011;

  PORTB |= LCD_E;
  delay_lcd();
  PORTB &= ~LCD_E;
  delay_lcd();
  PORTB |= LCD_E;
  delay_lcd();
  data1 = PINC;            // Get the data from the display's output register
  data2 = PIND;
  PORTB &= ~LCD_E;

  /*
  ** Return the read data
  **
  **
  */
  return( (data1&0b00111111) | ((data2&0b00001100) << 4) );

}

uint8 readStatus_lcd(void)
{
  PORTB = PINB & ~LCD_DI;

  return( readByte_lcd() );
}

void wait_lcd(void)
{
  while( (readStatus_lcd() & 0x80) );
}

// Purpose:       Write a byte of data to the specified chip
// Inputs:        chipSelect - which chip to write the data to
//                data - the byte of data to write
void writeByte_lcd( uint8 data )
{
  /*
  ** Set the data bus pins as output
  **
  */
  DDRC |= 0b00111111;
  DDRD |= 0b00001100;

  /*
  ** Set for writing
  **
  */
  PORTB = PINB & ~LCD_RW;

  /*
  ** Set the data on the port
  **
  */
  PORTC = data  & 0b00111111;
  PORTD = ((data >> 4) & 0b00001100) | (PIND & 0b11110011);

  /*
  ** Pulse the enable pin
  **
  */
  delay_lcd(); PORTB = PINB |  LCD_E;
  delay_lcd(); PORTB = PINB & ~LCD_E;

}

void segmentSelect_lcd( uint16 segmentNumber )
{
  /*
  ** Determine the chip number.  This is determined by bit 6 of.
  **  the segment number.  If this bit is ever true, then we 
  **  know we are addressing the second chip, otherwise we are 
  **  addressing the first chip.
  **
  */
  if( segmentNumber & 0x40 )
  {
    chipSelect_lcd(1);
  }
  else
  {
    chipSelect_lcd(0);
  }

}

void address_lcd( uint16 segmentNumber )
{
  segmentSelect_lcd( segmentNumber );

  /*
  ** Write the segment address.  This is the segment number
  **  0..63, or the 'Y' address according to the documentation.
  **  From the 'segmentNumber', it is bits 5..0 or 0x3f.  The
  **  command for writing the Y address is 0x40, to which we 
  **  then encode the actual segment value.
  **
  */
  wait_lcd();
  writeByte_lcd( 0x40 | (segmentNumber & 0b00111111) );

  /*
  ** Write the page address.  This is the page number 0..7 that
  **  occurs in bits 9..7 of the 'segmentNumber'.  We can get
  **  to them easily enough by masking and shifting.  The
  **  command for writing the page address is 0xb8, to 
  **  which we then encode the actual page value.
  **
  */
  wait_lcd();
  writeByte_lcd( 0xb8 | ((segmentNumber >> 7) & 0b00000111) );

  wait_lcd();

}

uint8 readSegment_lcd( uint16 segmentNumber )
{
  /*
  ** Address the lcd so that we're reading the correct data.
  **
  */
  address_lcd( segmentNumber );

  /*
  ** Set for data
  **
  */
  PORTB = PINB | LCD_DI;

  /*
  ** Read the data at that location
  **
  */
  return( readByte_lcd() );

}

void writeSegment_lcd( uint16 segmentNumber, uint8 dataByte )
{
  /*
  ** Address the lcd so that we're reading the correct data.
  **
  */
  address_lcd( segmentNumber );

  /*
  ** Set for data
  **
  */
  PORTB = PINB | LCD_DI;

  /*
  ** Write the data to that location
  **
  */
  writeByte_lcd( dataByte );

}

// Purpose: // Purpose:       Fill the LCD screen with the passed in color.
//                Works much faster than drawing a rectangle to fill the screen.
// Inputs:        ON - turn all the pixels on
//                OFF - turn all the pixels off
// Dependencies:  glcd_writeByte()
void fillScreen_lcd( uint8 fillByte )
{
  uint16 i, j;

  for( i = 0; i < 1024; i += 64 )
  {
    writeSegment_lcd( i, fillByte );

    // Loop through the vertical pages
    for( j = 1; j < 64; j++ )
    {
      wait_lcd();
      PORTB = PINB | LCD_DI;
      writeByte_lcd( fillByte );
    }

  }
  
}







/*************************************************************************/
/*************************************************************************/
/*************************************************************************/


const uint8 characters[] =
{
  0b00001110, // 0
  0b00010001,
  0b00001110,
  0b00000000,

  0b00010010, // 1
  0b00011111,
  0b00010000,
  0b00000000,

  0b00011101, // 2
  0b00010101,
  0b00010111,
  0b00000000,

  0b00010001, // 3
  0b00010101,
  0b00011111,
  0b00000000,

  0b00000111, // 4
  0b00000100,
  0b00011111,
  0b00000000,

  0b00010111, // 5
  0b00010101,
  0b00011101,
  0b00000000,

  0b00011111, // 6
  0b00010101,
  0b00011101,
  0b00000000,

  0b00000001, // 7
  0b00000001,
  0b00011111,
  0b00000000,

  0b00011111, // 8
  0b00010101,
  0b00011111,
  0b00000000,

  0b00000111, // 9
  0b00000101,
  0b00011111,
  0b00000000,

  0b00011110, // A
  0b00000101,
  0b00011110,
  0b00000000,

  0b00011111, // B
  0b00010100,
  0b00001000,
  0b00000000,

  0b00011111, // C
  0b00010001,
  0b00010001,
  0b00000000,

  0b00001000, // D
  0b00010100,
  0b00011111,
  0b00000000,

  0b00011111, // E
  0b00010101,
  0b00010001,
  0b00000000,

  0b00011111, // F
  0b00000101,
  0b00000001,
  0b00000000,

  0b00001010,
  0b00000100,
  0b00001010,
  0b00000000,

  0b00000100, // -
  0b00000100,
  0b00000100,
  0b00000000
};


uint16 segment;

void writeCharacter_lcd( uint8 character )
{
  if( character > 0x0F ) character = 0x10;

  character *= 4;

  writeSegment_lcd( segment, characters[character++] );
  writeByte_lcd(             characters[character++] );
  writeByte_lcd(             characters[character++] );
  writeByte_lcd(             characters[character]   );

  segment += 4;
}

void writeDecNumber_lcd( uint16 startingSegment, uint16 number )
{
  uint8  x = 0;
  uint16 y = 10000;

  segment = startingSegment;

  while( y > 0 )
  {
    x = 0;
    while( number >= y )
    {
      x++;
      number -= y;
    }

    writeCharacter_lcd( x );

    y /= 10;

  }

}

void writeHexNumber_lcd( uint16 startingSegment, uint16 number )
{
  uint8  x = 0;
  uint16 y = 0x1000;

  if( startingSegment == 0xffff )
  {
    segment += 4;
  }
  else
  {
    segment = startingSegment;
  }

  while( y > 0 )
  {
    x = 0;
    while( number >= y )
    {
      x++;
      number -= y;
    }

    writeCharacter_lcd( x );

    y >>= 4;

  }
}

/*************************************************************************/
/*************************************************************************/
/*************************************************************************/












uint8 lcd_x; // 0..127
uint8 lcd_y; // 0..63

void add_lcd_x( unsigned char x, unsigned char y )
{

  lcd_x += x;
  if( lcd_x > 127 )
  {
    lcd_x  = 0;
    lcd_y += y;
    if( lcd_y > 63 )
    {
      lcd_y = 0;
    }
  }

}

//      Initialize a graphic LCD. This must be called before any
//                other glcd function is used.
// Inputs:        The initialization mode
//                OFF - Turns the LCD off
//                ON  - Turns the LCD on
// Date:          5/28/2003
void init_lcd( uint8 on )
{
  DDRB = 0xff;

  lcd_x = 0;
  lcd_y = 0;

  chip_lcd = -1;

  /*
  ** Issue a reset pulse
  **
  */
  PORTB &= ~LCD_RST; _delay_ms(255);
  PORTB |=  LCD_RST; _delay_ms(255);

  /*
  ** Send an instruction to turn the display on
  **
  */
  chipSelect_lcd(0); wait_lcd(); writeByte_lcd( 0x3f );
                     wait_lcd(); writeByte_lcd( 0xc0 );
  chipSelect_lcd(1); wait_lcd(); writeByte_lcd( 0x3f );
                     wait_lcd(); writeByte_lcd( 0xc0 );

  fillScreen_lcd(0x00);            // Clear the display

}


// Purpose:       Turn a pixel on a graphic LCD on or off
// Inputs:        x - the x coordinate of the pixel
//                y - the y coordinate of the pixel
//                color - ON or OFF
// Output:        1 if coordinate out of range, 0 if in range
void pixel_lcd( uint8 x, uint8 y, uint8 color )
{

  uint8 data;
  uint8 chip = 0;  // Stores which chip to use on the LCD

  x &= 0b01111111;
  y &= 0b00111111;

  if(x > 63)      // Check for first or second display area
  {
     x   -= 64;
     chip = 1;
  }

  chipSelect_lcd(chip);

  /*
  ** Set for instruction
  **
  */
  x &= 0b01111111;   // Clear the MSB. Part of an instruction code
  x |= 0b01000000;   // Set bit 6. Also part of an instruction code

  wait_lcd();
  writeByte_lcd( x );                                 // Set the horizontal address

  wait_lcd();
  writeByte_lcd( ((y/8) & 0b10111111) | 0b10111000 );   // Set the vertical page address

  /*
  ** read data
  **
  */
  wait_lcd();
  PORTB |= LCD_DI;
  data = readByte_lcd();

//  printf( "x = %02x, chip = %u, data = %02x ", x, chip, data );

  if( color )
     data |=  (1<<(y%8));         // Turn the pixel on
  else                           // or
     data &= ~(1<<(y%8));         // turn the pixel off

//  printf( "data = %02x", data );

  wait_lcd();
  writeByte_lcd( x );      // Set the horizontal address

  wait_lcd();
  PORTB |= LCD_DI;
  writeByte_lcd( data );   // Write the pixel data


}


// Purpose:       Write text on a graphic LCD
// Inputs:        (x,y) - The upper left coordinate of the first letter
//                textptr - A pointer to an array of text to display
//                size - The size of the text: 1 = 5x7, 2 = 10x14, ...
//                color - ON or OFF
void text35_lcd( uint8 x, uint8 y, uint8 c, uint8 color )
{
  uint8 i, j, k;
  uint16 inFont = 0;

//  printf( "\r\ntext35_lcd %u %u %c ", x, y, c );

  /*
  ** Make sure the letter is within range.  If it is we pull
  **  the actual font data from the array
  **
  */
  if( (c >= (' '-1)) && (c <= '~') )
  {
    inFont = font3x5[c-(' '-1)];
  }

//  printf( "if:%lx", inFont );

  for( i = 0, k = 0; i < 3; i++ )
  {
//    printf( "\r\n" );

    for( j = 0; j < 5; j++, k++ )
    {
//      printf( "%d", j );

      if( inFont & (1<<k) )
      {
//        printf( "*" );
        pixel_lcd( x+i, y+j, 1 );
      }
      else
      {
//        printf( " " );
        pixel_lcd( x+i, y+j, 0 );
      }
    }
  }


}

void putc_lcd( uint8 c )
{
//  printf( "\r\nputc_lcd %c", c );

  text35_lcd( lcd_x, lcd_y, c, 1 );

  add_lcd_x(4,6); // advance to the next character position

}




/*************************************************************************/
/*************************************************************************/
/*************************************************************************/





uint16 y = 0;

int main(void)
{
  uint16 x = 0;
  uint8  y = 0;
  uint8  z = ' ';

//  _delay_ms(1000);

  DDRD |= LED;
  DDRD |= DB1;
  DDRD |= DB2;

  PORTD |= LED;


  /*
  ** Initialize the world
  **
  */
  init_timer();
  init_usart();
  init_lcd(1);

  /*
  ** Let'r rip!
  **
  */
  ENABLE_INTERRUPTS;

//  writeSegment_lcd( 0, readSegment_lcd(0) );

//  _delay_ms(5000);

  /*
  ** Main Control Loop
  **
  */
  forever
  {

//    PORTD |= LED;
//    _delay_ms(200);

//    PORTD &= ~LED;

//    fillScreen_lcd(x++);


//    TOGDB1;
//    txUsartPut(x++);
//    ENABLE_INTERRUPTS;

//    putc_lcd( x );
//    x++;
//    if( x > '9' ) x = '1';

#ifndef FILLSCREEN_TEST


    _delay_ms(500);

    fillScreen_lcd( x++ );

    writeHexNumber_lcd( 512, x );


#endif


#ifdef PIXEL_LINE_TEST

//    _delay_ms(1);

    pixel_lcd( x, y, z );

    x++;
    if( x == 128 )
    {
      x = 0;
      y++;

      if( y == 64 )
      {
        y = 0;
        z ^= 1;
      }
    }

    writeHexNumber_lcd( 512, x );


#endif


//    z = readSegment_lcd(x);

//    writeSegment_lcd(x,z+1);

#ifdef CHARACTER_TEST

    _delay_ms(1);

    text35_lcd( lcd_x, lcd_y, z, 1 );

    add_lcd_x( 4, 6 );

    z++;

    if( z > '}' ) z = ' ';

#endif

//    writeHexNumber_lcd( 512, x );
//    writeHexNumber_lcd( segment + 4, z );

//    z++;
//    writeSegment_lcd( x, z );
//    x += 4;

//    if( !(x & 1024) ) z++;


//    x &= 0x0f * 4;
//    z &= 0x0f;

//    for(ever_and_ever);

//    writeSegment_lcd( 2, data1 );
//    writeSegment_lcd( 3, data2 );

//    _delay_ms(5);

//    writeSegment_lcd( 0, x );
//
//    _delay_ms(500);

//    z = readSegment_lcd( 0 );
//    z = readSegment_lcd( 0 );

//    if( z == x ) 
//    {
//      writeHexNumber_lcd( 512, x );
//    }
//    else
//    {
//      writeHexNumber_lcd( 512, 0xffff );
//      writeHexNumber_lcd( -1,  x );
//      writeHexNumber_lcd( -1,  z );
//    }


//    writeHexNumber_lcd( 512,         x );
//    writeHexNumber_lcd( 512+128,     readSegment_lcd(0) );
//    writeHexNumber_lcd( segment + 4, readSegment_lcd(1) );
//    writeHexNumber_lcd( segment + 4, readSegment_lcd(2) );
//    writeHexNumber_lcd( segment + 4, readSegment_lcd(3) );
//    writeHexNumber_lcd( 512+256,     readSegment_lcd(4) );
//    writeHexNumber_lcd( segment + 4, readSegment_lcd(5) );
//    writeHexNumber_lcd( segment + 4, readSegment_lcd(6) );
//    writeHexNumber_lcd( segment + 4, readSegment_lcd(7) );

//    _delay_ms(500);

//    y++;

//    x++;
//    x &= 0x07;

//    if( !x ) _delay_ms(1000);

//    writeSegment_lcd( x, x );
//    x++;


//     UDR0 = x++;
//     if( TX_USART_RUNNING )
//     {
//       UDR0 = 0x55;
//     }


  }
}

